<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>餐厅扫码点餐系统 - SmartOrder</title>
    <link rel="stylesheet" href="/css/element-ui.css">
    <style>
        body {
            font-family: Avenir, Helvetica, Arial, sans-serif;
            color: #2c3e50;
        }

        a {
            color: #2c3e50;
            text-decoration: none;
        }

        a:hover {
            color: #409eff;
            text-decoration: none;
        }

        .footer {
            margin-top: 20px;
            background-color: #f56c6c;
            color: white;
            font-size: 18px;
            line-height: 54px;
            text-align: center;
        }
    </style>
</head>
<body>
<div id="app" style="text-align: center;">
    <h1>餐厅扫码点餐系统（SmartOrder）后台管理</h1>
    <div style="margin-top: 20px;">
        <a href="dining_table.html">餐桌管理</a> |
        <a href="category.html">菜品类别管理</a> |
        <a href="dish.html">菜品管理</a> |
        <a href="check_out.html">结账管理</a> |
        <a href="/">返回前台</a>
    </div>
    <div style="margin-top: 20px;">
        <el-button type="primary" @click="addTable">增加餐桌</el-button>
    </div>
    <div style="width: 70%; margin: 0 auto;">
    <el-table :data="diningTables" border style="width: 100%; margin-top: 20px;">
        <el-table-column prop="id" label="餐桌号"></el-table-column>
        <el-table-column label="二维码图片">
            <template slot-scope="scope">
                <el-image style="width: 100px; height: 100px" :src="scope.row.qrCodePath" fit="fill"></el-image>
            </template>
        </el-table-column>
        <el-table-column label="管理">
            <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" circle size="mini" disabled></el-button>
                <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleteTable(scope.row.id)"></el-button>
            </template>
        </el-table-column>
    </el-table></div>
    <div class="footer">
        当前页面由AI自动生成，可能需要进一步优化或调整。
    </div>
</div>
<!-- 引入Vue.js -->
<script src="/js/vue.js"></script>
<!-- 引入Element-UI -->
<script src="/js/element-ui.js"></script>
<!-- 引入AXIOS -->
<script src="/js/axios.min.js"></script>
<script>
    console.log('页面加载中...');
    new Vue({
        el: '#app',
        data() {
            return {
                diningTables: []
            };
        },
        created() {
            console.log('获取餐桌列表...');
            this.fetchTables();
        },
        methods: {
            fetchTables() {
                axios.get('/api/diningTables')
                    .then(response => {
                        console.log('获取餐桌列表成功:', response.data);
                        this.diningTables = response.data;
                    })
                    .catch(error => {
                        console.error('获取餐桌列表失败:', error);
                    });
            },
            addTable() {
                console.log('增加餐桌...');
                axios.post('/api/diningTables/addNew')
                    .then(response => {
                        console.log('增加餐桌结果:', response.data);
                        if (response.data === 1) {
                            this.$message.success('增加成功');
                        } else {
                            this.$message.error('增加失败');
                        }
                        this.fetchTables();
                    })
                    .catch(error => {
                        console.error('增加餐桌失败:', error);
                        this.$message.error('增加失败');
                    });
            },
            deleteTable(tableId) {
                console.log('删除餐桌:', tableId);
                this.$confirm('确认删除此餐桌?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post(`/api/diningTables/delete?tableId=${tableId}`)
                        .then(response => {
                            console.log('删除餐桌结果:', response.data);
                            if (response.data === 1) {
                                this.$message.success('删除成功');
                            } else {
                                this.$message.error('删除失败');
                            }
                            this.fetchTables();
                        })
                        .catch(error => {
                            console.error('删除餐桌失败:', error);
                            this.$message.error('删除失败');
                        });
                }).catch(() => {
                    console.log('取消删除');
                });
            }
        }
    });
</script>
</body>
</html>